package com.java.androidmarket.config;

import com.google.common.util.concurrent.RateLimiter;
import com.java.androidmarket.interceptor.JWTInterceptor;
import com.java.androidmarket.interceptor.RateLimiterInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



/**
 * description:
 * author:张腾
 * date:2021-04-25
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public JWTInterceptor getJWTInterceptor(){
        return new JWTInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getJWTInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/check")
//                .excludePathPatterns("/user/test")
                .excludePathPatterns("/comment/getComment")
                .excludePathPatterns("/stock/*")
                .excludePathPatterns("/killStock/*")
                .excludePathPatterns("/goods/*")
                //放行swagger静态资源
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/swagger-ui.html/**")
                .excludePathPatterns("/webjars/**")

                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/*.jpg","/*.png","/*.jpeg","/*.gif");

        registry.addInterceptor(new RateLimiterInterceptor(RateLimiter.create(30)))
                .addPathPatterns("/order/*");
    }
}
